package com.leslie.hhcommunity.controller;

import com.leslie.hhcommunity.util.CommunityUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.controller
 * @ClassnName:AlphaController
 * @Description:
 * @Date 2022/1/1  20:41
 */
@Controller
@RequestMapping("/alpha")
public class AlphaController {

	/**
	 * 路径中带参数 /123
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(path = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String getStudent(@PathVariable("id") int id) {
		System.out.println("id= " + id);
		return "success";
	}

	@GetMapping("/cookie/set")
	@ResponseBody
	public String setCookie(HttpServletResponse response) {
		// 创建cookie
		Cookie cookie = new Cookie("code", CommunityUtil.generateUUID());
		// 设置cookie生效范围
		cookie.setPath("/community/alpha");
		// 设置cookie的生存时间
		cookie.setMaxAge(60 * 10);
		// 发送cookie
		response.addCookie(cookie);
		return "set cookie";
	}

	@GetMapping("/cookie/get")
	@ResponseBody
	public String getCookie(@CookieValue("code") String code) {
		System.out.println(code);
		return "get cookie";
	}

	@GetMapping("/session/set")
	@ResponseBody
	public String setSession(HttpSession session) {
		session.setAttribute("id", 1);
		session.setAttribute("name", "hh");

		return "set session";
	}

	@GetMapping("/session/get")
	@ResponseBody
	public String getSession(HttpSession session) {
		System.out.println(session.getAttribute("id"));
		System.out.println(session.getAttribute("name"));
		return "get session";
	}

	@PostMapping("/ajax")
	@ResponseBody
	public String testAjax(String name, int age) {
		System.out.println(name);
		System.out.println(age);
		return CommunityUtil.getJSONString(0, "操作成功！");
	}

	public void testGit(){
		System.out.println("git hhh");
		System.out.println("GIT HHH");
		System.out.println("Hello");
		System.out.println("HiHi");
	}
}
